package autosimmune.agents.cells;

import repast.simphony.annotate.AgentAnnot;
import repast.simphony.engine.schedule.ScheduledMethod;
import autosimmune.agents.Antibody;
import autosimmune.agents.pathogens.Allergen;
import autosimmune.agents.pathogens.Bacteria;
import autosimmune.agents.pathogens.Virus;
import autosimmune.defs.BacteriaStates;
import autosimmune.defs.CitokineNames;
import autosimmune.defs.EnvParameters;
import autosimmune.defs.MacrophageStates;
import autosimmune.defs.PAMPS;
import autosimmune.env.Environment;
import autosimmune.env.Global;
import autosimmune.utils.Affinity;
import autosimmune.utils.Pattern;

/**
 * Classe representando a Célula Macrófago
 * @author maverick
 *
 */
@AgentAnnot(displayName = "Macrophage Cell")
public class Macrophage extends APC {

	/** Representação dos estados internos da célula */
	private MacrophageStates state;
	
	private int lifetime;
	private int digestingTicks=10;
	private int mk1duration;
	
	/**
	 * Construtor da Classe
	 * @param z Zona a qual o macrófago pertence inicialmente
	 * @param self Peptídeo self que identifica o macrófago
	 * @param x Coordenada X inicial
	 * @param y Coordenada Y inicial
	 */
	public Macrophage(Environment z, int x, int y) {
		super(z, x, y, new Pattern(Global.getInstance().getStringParameter(EnvParameters.MACROPHAGE_SELF_PATTERN)));
		state = MacrophageStates.ACTIVE;
		initiateParameters();
	}
	
	private void initiateParameters(){
		this.lifetime = Global.getInstance().getIntegerParameter(EnvParameters.MACROPHAGE_LIFETIME);
		this.mk1duration = Global.getInstance().getIntegerParameter(EnvParameters.MACROPHAGE_MK1_DURATION);
		
	}
	
	/**
	 * Método que executa o passo do macrofago a cada tick
	 */
	@ScheduledMethod(start = 0, interval = 1)
	public void step(){
		
		tick();
		if (this.getTicks() > lifetime && this.state==MacrophageStates.ACTIVE){
			this.state=	MacrophageStates.APOPTOSIS;
		}
		
		switch(state){
		
		case DIGESTING: {
			--this.digestingTicks;
			if(this.digestingTicks<=0){
				this.state=MacrophageStates.ACTIVE;
				this.digestingTicks=10;
			}
			randomWalk(true);
			
		}
		break;
		
		case ACTIVE: {
				randomWalk(true);
				//followCitokineByGradient(true, CitokineNames.MICENZYME);
				if(getEspecificNeighbors(Bacteria.class).size()>0){
					for(Bacteria b: getEspecificNeighbors(Bacteria.class, 1)){
						if(b.isInactive()){
							b.foundToDie();
							System.out.println("matou bacteria");
							this.state=MacrophageStates.DIGESTING;
							break;
						}
					}
				}
				
				if(getEspecificNeighbors(Allergen.class).size()>0){
					for(Allergen a: getEspecificNeighbors(Allergen.class, 1)){
							a.capturado();
							this.state=MacrophageStates.DIGESTING;
							break;
					}
				}
				
			}
		break;
		
		case APOPTOSIS: {
			this.die();
		}
		break;
	
	}
		
	}

	@Override
	public boolean infectedBy(Virus virus) {
		return false;
	}

	@Override
	public void contact(boolean b) {
		//TODO parametrizar fatores de prolongamento de vida
		mk1duration+=10;
		lifetime+=10;
	}
	

}
